package linkedlist;

/**
 * @Author: 海琳琦
 * @Date: 2021/12/30 13:51
 * 给你单链表的头节点 head，请你反转链表，并返回反转后的链表。
 * https://leetcode-cn.com/problems/reverse-linked-list/
 */
public class ReverseList {

    static class ListNode {
        int val;

        ListNode next;

        ListNode() {

        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode reverseList(ListNode head) {
        ListNode preNode = null;
        ListNode curNode = head;
        ListNode tmp;
        while (curNode != null) {
            tmp = curNode.next;
            curNode.next = preNode;
            preNode = curNode;
            curNode = tmp;
        }
        return preNode;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        ListNode listNode = reverseList(head);
        System.out.println();
    }
}
